<template>
  <div class="login">
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="email"
        label="邮箱"
        placeholder="邮箱"
        :rules="[{ email, message: '请填写手机号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import Vue from "vue";
import axios from "axios";
import { NavBar, Form, Field, Toast } from "vant";
Vue.use(NavBar);
Vue.use(Form);
Vue.use(Field);
export default {
  data() {
    return {
      email: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
      username: "",
      password: "",
    };
  },
  computed: {},
  watch: {},

  methods: {
    onSubmit(values) {
      axios
        .get(
          `http://localhost:3000/login?email=${values.email}&password=${values.password}`,
          {
            withCredentials: true,
          }
        )
        .then((result) => {
          if (result.data.code == 200) {
            Toast.success("登陆成功");
            localStorage.setItem("token", result.data.token);
            this.$router.push("/home");
          } else {
            Toast.fail("邮箱或者密码错误");
          }
        });
    },
    onClickLeft() {
      this.$router.go(-1);
    },
  },
  created() {},
  mounted() {},
  components: {},
};
</script>
<style scoped>
</style>